/*
 * reportesC.java
 *
 * Created on 29 de agosto de 2008, 01:06 PM
 */
package administrador;
import java.util.Calendar;
import net.sf.jasperreports.engine.JRException;
/**
 *
 * @author  Jorge
 */
public class reportesC extends javax.swing.JFrame {

    /** Creates new form reportesC */
        calendario.JCalendar jCalendar = null;
        Conexion conexion;
        String op="";
    public reportesC(Conexion conexion,String op) {
        initComponents();
        this.op=op; //
        if(op.equals("comprasxCliente")){
            comboPago.setVisible(false);
            setTitle("Compras x Cliente");
        }else if(op.equals("cuentasCobrar")){
            setTitle("Cuentas x Cobrar");
        }else if(op.equals("historialVentas")){
            comboPago.setVisible(false);
            setTitle("Historial de Ventas");
            txt_cedula.setEnabled(false);
        }

        this.conexion=conexion;
        setVisible(true);
        jCalendar = new calendario.JCalendar(this, "Calendario", true);
    }
    java.sql.Date fecha1=null;
    java.sql.Date fecha2=null;
    public void sacarFecha(int opcion) {
        Calendar cDate = jCalendar.getCalendar();
        if (cDate == null) {
            cDate = Calendar.getInstance();
        }
        jCalendar.setCalendar(cDate);
        jCalendar.setVisible(true);
        if (jCalendar.isOkPressed()) {
            if (opcion == 1) {
                fecha1 = new java.sql.Date(jCalendar.getDate().getTime());
                System.out.println(fecha1);
                txt_FechaIni.setText(fecha1.toString());
            } else if (opcion == 2) {
                fecha2 = new java.sql.Date(jCalendar.getDate().getTime());
                txt_fchFin.setText(fecha2.toString());
            }
            jCalendar.setVisible(false);
        }
    }
    public void imprimir(String nombreArchivo){
        if(txt_FechaIni.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha de Inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
            if(txt_fchFin.getText().equals("")){
                javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha Fin","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
            }else{
                     if(txt_FechaIni.getText().compareTo(txt_fchFin.getText())<=0){
                            String path = "./Reportes/";
                            java.util.Map parametros = new java.util.HashMap();
                            if(txt_cedula.getText().equals("")){
                                parametros.put("cod_cliente","%");
                            }else{
                                parametros.put("cod_cliente",txt_cedula.getText());
                            }
                            if (comb_Comprobante.getSelectedItem().equals("Factura")){
                                parametros.put("tipo_Comprobante","F");
                            }else{
                                parametros.put("tipo_Comprobante","N");
                            }
                            parametros.put("fecha_Inicio",fecha1);
                            parametros.put("fecha_Fin",fecha2);
                            try {
                                    generaReporte(path+ nombreArchivo,parametros);
                            } catch (Exception e) {
                                    System.out.println(e.getMessage());
                            }  
                    }else{
                        javax.swing.JOptionPane.showMessageDialog(this,"La fecha fin no puede ser menor que la fecha de inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
                    }
            }
        }
    }
    public String consultarIva(){
        String iva="";
        java.util.Vector vecConsulta=conexion.consulta("select iva from parametros order by codigo desc limit 1");
        java.util.Vector vecInterno=new java.util.Vector();
        for(int i=0;i<vecConsulta.size();i++){
                vecInterno=(java.util.Vector)vecConsulta.elementAt(i);
                iva=vecInterno.elementAt(0).toString();
        }
        return iva;
    };
     public String Cortar(String numero){
        String numeros="";
        for(int i=0;i<numero.length();i++){
            String caracter=numero.substring(i,i+1);
            if(caracter.equals(".") || caracter.equals(",")){
            }else{
               numeros+=caracter;
            }
        }
        return numeros;
    }
    public void imprimirHistorialV(String nombreArchivo){
        double porcentajer=Double.parseDouble(consultarIva());
        String ivaCompleto="1."+Cortar(String.valueOf(porcentajer)); //
        if(txt_FechaIni.getText().equals("")){
            javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha de Inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
        }else{
            if(txt_fchFin.getText().equals("")){
                javax.swing.JOptionPane.showMessageDialog(this,"Ingresar Fecha Fin","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
            }else{
                     if(txt_FechaIni.getText().compareTo(txt_fchFin.getText())<=0){
                            String path = "./Reportes/";
                            java.util.Map parametros = new java.util.HashMap();
                            if (comb_Comprobante.getSelectedItem().equals("Factura")){
                                parametros.put("tipo_Comprobante","F");
                            }else{
                                parametros.put("tipo_Comprobante","N");
                                parametros.put("valIvaEnt",Double.parseDouble(ivaCompleto));
                                
                            }
                            parametros.put("fecha_Inicio",fecha1);
                            parametros.put("fecha_Fin",fecha2);
                            try {
                                    generaReporte(path+ nombreArchivo,parametros);
                            } catch (Exception e) {
                                    System.out.println(e.getMessage());
                            }  
                    }else{
                        javax.swing.JOptionPane.showMessageDialog(this,"La fecha fin no puede ser menor que la fecha de inicio","Precaución",javax.swing.JOptionPane.WARNING_MESSAGE); 
                    }
            }
        }
    }
    public void  generaReporte(String path,java.util.Map parametros) throws JRException{
        net.sf.jasperreports.engine.JasperReport report = net.sf.jasperreports.engine.JasperCompileManager.compileReport(path);
        net.sf.jasperreports.engine.JasperPrint print = net.sf.jasperreports.engine.JasperFillManager.fillReport(report, parametros, conexion.getconexion());
        net.sf.jasperreports.view.JasperViewer.viewReport(print,false);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        btn_genera = new javax.swing.JButton();
        comboPago = new javax.swing.JComboBox();
        comb_Comprobante = new javax.swing.JComboBox();
        txt_cedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        txt_FechaIni = new javax.swing.JTextField();
        txt_fchFin = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(reportesC.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"), resourceMap.getColor("jPanel1.border.titleColor"))); // NOI18N
        jPanel1.setForeground(resourceMap.getColor("jPanel1.foreground")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setName("jPanel2"); // NOI18N

        btn_genera.setText(resourceMap.getString("btn_genera.text")); // NOI18N
        btn_genera.setName("btn_genera"); // NOI18N
        btn_genera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_generaActionPerformed(evt);
            }
        });

        comboPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pagan", "No_Pagan" }));
        comboPago.setName("comboPago"); // NOI18N

        comb_Comprobante.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura", "Nota de Venta" }));
        comb_Comprobante.setName("comb_Comprobante"); // NOI18N

        txt_cedula.setName("txt_cedula"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txt_FechaIni.setEditable(false);
        txt_FechaIni.setName("txt_FechaIni"); // NOI18N

        txt_fchFin.setEditable(false);
        txt_fchFin.setName("txt_fchFin"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(19, 19, 19)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txt_fchFin)
                                    .addComponent(txt_FechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4)
                                .addGap(4, 4, 4))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btn_genera, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comb_Comprobante, 0, 119, Short.MAX_VALUE)
                    .addComponent(txt_cedula, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(comboPago, 0, 119, Short.MAX_VALUE))
                .addGap(34, 34, 34))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_FechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txt_cedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addComponent(jButton1))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txt_fchFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(comb_Comprobante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comboPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(btn_genera, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-532)/2, (screenSize.height-223)/2, 532, 223);
    }// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    sacarFecha(1);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    sacarFecha(2);
}//GEN-LAST:event_jButton2ActionPerformed
private void btn_generaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_generaActionPerformed
        if(op.equals("comprasxCliente")){
               imprimir("ComprasxCliente/"+"contenedor.jrxml");  //compras por cada uno de los clientes
        }else if(op.equals("cuentasCobrar")){
            if(comboPago.getSelectedItem().equals("Pagan")){
                imprimir("Cuentas Cobrar/"+"contenedor.jrxml");  //clientes que estan pagando 
            }else{
                imprimir("Cuentas Cobrar/"+"contenedorsP.jrxml"); //clientes que no han  echo ningun pago
            }
        }else if(op.equals("historialVentas")){
            if (comb_Comprobante.getSelectedItem().equals("Factura")){
                      imprimirHistorialV("Historial de Ventas/"+"contenedor.jrxml");  //compras por cada uno de los clientes
            }else{
                      imprimirHistorialV("Historial de Ventas/"+"contenedorN.jrxml");  //compras por cada uno de los clientes
                                
           }
            
            
        }
}//GEN-LAST:event_btn_generaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_genera;
    private javax.swing.JComboBox comb_Comprobante;
    private javax.swing.JComboBox comboPago;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txt_FechaIni;
    private javax.swing.JTextField txt_cedula;
    private javax.swing.JTextField txt_fchFin;
    // End of variables declaration//GEN-END:variables
}
